<html>
    <head>
        <title>Create Connection</title>
        <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/dojo/1.8.3/dijit/themes/claro/claro.css" media="screen" />
		<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/dojo/1.8.3/dojox/grid/resources/claroGrid.css" />
    </head>
    <body class="claro">
        <script>
            dojoConfig = {
                isDebug: true,
                async: true,
                parseOnLoad: false,
                baseUrl: "js/",
                packages: [
                    {name: "my", location:"my"}
                ]};
        </script>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/dojo/1.8.3/dojo/dojo.js"></script>
        <script>
            require(["dojo/ready", "dojox/xml/DomParser", "dijit/form/Textarea", "dijit/form/TextBox", "dijit/form/Button",
                "dijit/layout/ContentPane", "dijit/layout/BorderContainer"],
                function(ready, DomParser, Textarea, TextBox, Button, ContentPane, BorderContainer) {
                    ready(function() {
                        var bc = new BorderContainer({style: "height: 100%; width: 100%;"});
                        var topPane = new ContentPane({
                            region: "top",
                            style: "height: 10%; width: 100%;",
                            content: ""});
                        var trailing = new ContentPane({
                            region: "trailing",
                            style: "height: 100%; width: 20%;",
                            content: ""});
                        var center = new ContentPane({
                            region: "center",
                            style: "height: 100%; width: 78%;",
                            content: ""});
                        bc.addChild(topPane);
                        bc.addChild(trailing);
                        bc.addChild(center);

                        var xmlText = new Textarea({
                            style: "height: 80%; width: 100%;"
                        });

                        var test = new Button({
                            label: "Check",
                            onClick: function() {
                                Check(xmlText.value);
                            }});

                        var commit = new Button({
                            label: "Commit",
                            disabled: true,
                            onClick: function() {

                            }});

                        function Check(data){
                            if (data == ""){
                                return "Need XML";
                            }
                            structure = DomParser.parse(data);
                            test = iter(structure);
                            base = {};
                            others = [];
                            baseUrl = "";
                            for (var i = 0; i < test.length; i++) {
                                if (test[i].nodeName == "base") {
                                    base = test[i].nodeName;
                                    for (var l = 0; l < base.attributes.length; l++) {
                                        if (base.attributes[l].nodeName == "url") {
                                            baseUrl = baseattributes[l].nodeValue;
                                        }
                                    }
                                } else if (test[i].nodeName == "scope" || test[i].nodeName == "variable") {
                                    for (var m = 0; m < test[i].attributes; m++) {
                                        type = "";
                                        ext = "";
                                        tag = test[i].nodeName;
                                        if (test[i].attributes[m].nodeName == "type") {
                                            type = test[i].attributes[m].nodeValue;
                                        } else if (test[i].attributes[m].nodeName == "ext") {
                                            ext = test[i].attributes[m].nodeValue;
                                        }
                                    others.push({tag: tag, type: type, ext: ext});
                                }
                            }
                            for (var j = 0; j < others.length; j++) {
                                if (others[j]['tag'] == "scope") {
                                    testJson(baseUrl, others[j]);
                                } else if (others[j]['tag'] == "variables") {
                                    // Run a Dialog box to get what the variable should be.
                                    testJson(baseUrl, others[j]);


                            console.log(structure);
                        }

                        function testJSON(baseUrl, tag) {
                            for (var k = 0; k < tag.attributes; k++) {
                                if ( tag['type'] == "GET") {
                                    request.get(baseUrl + tag['ext']).then(
                                        function(data) {
                                            try {
                                                JSON.parse(data);
                                            } catch(err) {
                                                return "Not getting JSON from source: " + tag['ext'];
                                            }});
                                } else if (tag['type'] == "POST") {
                                    request.post(baseUrl + tag['ext']).then(
                                        function(data) {
                                            try {
                                                JSON.parse(data);
                                            } catch(err) {
                                                return "Not getting JSON from source: " + tag['ext'];
                                            }});
                                } else {
                                    return "Only accepts GET or POST: " + tag['ext'];
                                }
                           }

                        }


                        function iter(tag){
                            var state = [];
                            try {
                                if (tag.childNodes.length == 0) {
                                    state = [tag];
                                } else {
                                    var list = [];
                                    for (var i = 0; i < tag.childNodes.length; i++) {
                                        var child = tag.childNodes[i];
                                        var items = iter(child);
                                        for (var j = 0; j < items.length; j++) {
                                            var item = items[j];
                                            console.log(item);
                                            list.push(item);
                                        }
                                    }
                                    tag.childNodes = [];
                                    list.push(tag);
                                    state = list;

                                }
                            } catch(err) {
                                state = [tag];
                            } finally {
                                return state;
                            }
                            return state

                        }

                        center.set("content", [
                            xmlText.domNode,
                            test.domNode,
                            commit.domNode]);

                        document.body.appendChild(bc.domNode);
                        bc.startup();

                    });
            });
        </script>
    </body>
</html>
